// pages/sendoutGift/sendoutGift.js
var that,
  api = new (require('../../utils/api.js')),
  headimg = api.getimgurl(),
  app = getApp(),
  util = new (require('../../utils/util.js'));
Page({

  /**
   * 页面的初始数据
   */
  data: {
    recordArr:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    if (app.logininfo) {
      that.getdata();
    } else {
      util.setwatcher(app, this.afterlogin)
    }
  },
  afterlogin: {
    logininfo: res => {
      that.getdata();
    }
  },
  getdata(){
    wx.showLoading({
      title: '加载中...',
    })
    let {page} = that.data
    api.getshareRecord(page,res=>{
      wx.hideLoading()
      if(res.data.code==0){
        let {canNum,nextPage,recordArr} = res.data.entity.data
        that.setData({canNum,nextPage,recordArr},()=>{
          if(that.data.canNum>0){
            api.usershare(res1=>{
             let {shareKey}=res1.data.entity.data
             that.setData({shareKey,nextPage})
            })
          }
        })
      }
    })
    let userinfo=wx.getStorageSync('userInfo')
    that.setData({userinfo})
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let { nextPage, page } = that.data
    if (nextPage == 1) {
      page = page + 1
      that.setData({ page }, () => {
        that.getdata()
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})